﻿using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using Util;
namespace UtilEditors
{
    public class FileEditers :Inspector
    {
        List<Object> GetObject = new List<Object>();
        public override void Init(SubsidiaryWindow win,int windownID)
        {
            rect = new Rect(110,220,120,200);
            MainWin = win;
            ID = windownID;
            rect.x = rect.x * ID;
        }
        public override void SelectionChange()
        {
            if(inquireName.isTrue())
            {
                return;
            }
            Object asset = Selection.activeObject;
            GetObject = new List<Object>();
            if(asset is DefaultAsset)
            {
                GetObject.Add(asset);
            }
            GameObject go = Selection.activeGameObject;
            if(go)
            {
                GetObject.Add(go);
            }
            bool cs = GetObject.Count > 0;
            if(IsShow != cs)
            {
                IsShow = cs;
                if(IsShow)
                {
                    if(ID != 0)
                    {
                        RankingPos();
                    }
                }
            }
        }
        bool inquire;
        string inquireName;
        public override void OnIGUI()
        {
            查找();
        }
        List<Object> FindObj = new List<Object>();
        bool isShowFindResult;
        TimerEditor.TimerProc tp;
        bool isSceneAll;
        public void 查找()
        {
            Object SelectionObj = Selection.activeObject;
            string title = "";
            if(SelectionObj)
            {
                title = SelectionObj.name;
            }
            bool isScneneObj;
            if(SelectionObj is DefaultAsset)
            {
                isScneneObj = false;
            }
            else
            {
                isScneneObj = true;
            }

            ComX(() =>
            {
                Text(title);
                if(isScneneObj)
                {
                    Toggle("全场景",ref isSceneAll);
                    if(isSceneAll)
                    {
                        if(Button("查询",GUILayout.MaxWidth(50)))
                        {
                            查找开始();
                        }
                    }
                }
            });
            if(isScneneObj && isSceneAll)
            {
                Inpt("",ref inquireName);
            }
            else
            {//少量查找,智能一点不用点按钮
                ChangeCheck(() =>
                {
                    Inpt("",ref inquireName);
                },() =>
                {
                    TimerEditor.Instance.Add(0.5f,ref tp,查找开始);
                });
            }
            if(FindObj.Count > 0)
            {
                ShowList("",ref isShowFindResult,FindObj);
            }
        }

        public Dictionary<int,string> layerNmae = new Dictionary<int,string>();
        public string GetLayerNmae(int id)
        {
            string name;
            if(layerNmae.TryGetValue(id,out name))
            {
                return name;
            }
            else
            {
                name = LayerMask.LayerToName(id);
                layerNmae.Add(id,name);
                return name;
            }
        }
        public void 查找开始()
        {
            try
            {
                查找开始s();
            }
            catch(System.Exception ex)
            {

            }
        }

        public void 查找开始s()
        {
            FindObj.Clear();
            if(inquireName.Length >= 2)
            {
                Object SelectionObj = Selection.activeObject;
                List<string> keywordl = new List<string>(inquireName.toSplit(' '));
                for(int i = keywordl.Count - 1; i >= 0; i--)
                {
                    keywordl[i] = keywordl[i].Replace(" ","");
                    if(keywordl[i].isNull())
                    {
                        keywordl.RemoveAt(i);
                    }
                }
                string[] keyword = keywordl.ToArray();
                if(SelectionObj is DefaultAsset)
                {
                    string fullPath = AssetDatabase.GetAssetPath(SelectionObj);
                    // string fullPath = "Assets/Models/21/21000001/";  //路径 
                    //获取指定路径下面的所有资源文件  
                    if(Directory.Exists(fullPath))
                    {
                        DirectoryInfo direction = new DirectoryInfo(fullPath);
                        string findKey = string.Format("*{0}*",keyword[0]);
                        FileInfo[] files = direction.GetFiles(findKey,SearchOption.AllDirectories);
                        //Debug.Log(files.Length); 
                        for(int i = 0; i < files.Length; i++)
                        {
                            if(files[i].Name.EndsWith(".meta"))
                            {
                                continue;
                            }
                            if(StringTool.to_find_MultiKey(files[i].Name,keyword))
                            {
                                string npath = files[i].FullName.toUintyPath();
                                Object mObj = AssetDatabase.LoadAssetAtPath<Object>(npath);
                                if(mObj)
                                {
                                    FindObj.Add(mObj);
                                }
                            }
                        }
                        if(FindObj.Count > 0)
                        {
                            isShowFindResult = true;
                        }
                    }
                }
                else
                {
                    if(isSceneAll)//全场景查询
                    {
                        UtilEditor.AllScnenAllFind((tf) =>
                        {
                            AddTf(tf,keyword);
                            return false;
                        });
                    }
                    else
                    {
                        List<Transform> FindObj2 = new List<Transform>();
                        GameObject go = Selection.activeGameObject;
                        go.transform.GetComponentsInChildren(true,FindObj2);

                        for(int i = 0; i < FindObj2.Count; i++)
                        {
                            var tf = FindObj2[i];
                            AddTf(tf,keyword);
                        }
                    }
                    if(FindObj.Count > 0)
                    {
                        isShowFindResult = true;
                    }
                }
            }
            MainWin.Repaint();
        }

        public void AddTf(Transform tf,string[] keyword)
        {
            using(zstring.Block())
            {
                zstring name = tf.name;
                zstring comName = "";
                var coms = tf.GetComponents<Component>();
                var comslist = new List<string>();
                for(int i = 0; i < coms.Length; i++)
                {
                    if(coms[i])
                    {
                        zstring typeName = coms[i].GetType().ToString();
                        comslist.Add(typeName);
                        comName += typeName;
                    }
                    else
                    {
                        comslist.Add("");
                    }
                }
                zstring info = zstring.Format("{0}{1}{2}{3}",name,GetLayerNmae(tf.gameObject.layer),tf.tag,comName);
                if(StringTool.to_find_MultiKey(info,keyword))
                {
                    for(int n = 0; n < coms.Length; n++)
                    {
                        if(coms[n])
                        {
                            string typeName = comslist[n];
                            if(StringTool.to_find_Key(typeName,keyword))
                            {
                                FindObj.Add(coms[n]);
                                return;
                            }
                        }
                    }
                    FindObj.Add(tf);
                }
            }
        }
    }
}